import src.de.checker.hochzeit.konstanten.Konstanten;
import java.util.logging.Logger;
import src.de.checker.hochzeit.domain.util.*;
import src.de.checker.hochzeit.domain.user.*;
import src.de.checker.hochzeit.konstanten.*
import com.google.appengine.api.datastore.*
import static com.google.appengine.api.datastore.FetchOptions.Builder.*

Logger log = Logger.getLogger('DefBudgetController');


if(params?.index ==~ /(?i)true/)
{
	index()
}
if(params?.createbudget ==~ /(?i)true/)
{
	createbudget()
}
if(params?.indexkat ==~ /(?i)true/)
{
	indexkat()
}
if(params?.createkat ==~ /(?i)true/)
{
	createkat()
}
if(params?.deletebudget ==~ /(?i)true/)
{
	deletebudget()
}

private void index()
{
	def query = new Query(Konstanten.DEFAULT_BUDGET)
	PreparedQuery preparedQuery = datastore.prepare(query)
 	def entities = preparedQuery.asList( withLimit(300) )
	
 	def query1 = new Query(Konstanten.BKATEGORIE)
	PreparedQuery preparedQuery1 = datastore.prepare(query1)
 	def entities1 = preparedQuery1.asList( withLimit(300) )
 	
 	request['list'] = entities
 	request['kat'] = entities1
 	
 	forward '/admin/budget.gtpl'
}

private void indexkat()
{
	def query1 = new Query(Konstanten.BKATEGORIE)
	PreparedQuery preparedQuery1 = datastore.prepare(query1)
 	def entities1 = preparedQuery1.asList( withLimit(300) )
 	
 	request['list'] = entities1
 	
 	forward '/admin/budgetkat.gtpl'
}
private void createkat()
{
	Entity entity = new Entity(Konstanten.BKATEGORIE)
	entity['id'] = new Integer(params.id)
	entity['beschreibung'] = params.beschreibung
	entity.save()
	redirect '/admin/budgetkat'
}
private void createbudget()
{
	Entity entity = new Entity(Konstanten.DEFAULT_BUDGET)
	entity['kurzbeschreibung'] = params.kurzbeschreibung
	entity['anmerkung'] = params.anmerkung
	if(params.kalkulierteKosten)
	{
		try
		{
			Double d = new Double(params.kalkulierteKosten)
			entity['kalkulierteKosten'] = d
		}
		catch(Exception e){}
	}
	entity['kategorie'] = KeyFactory.stringToKey(params.kategorie)
	entity.save()
	redirect '/admin/defbudget'
}
private void deletebudget()
{
	Key item = KeyFactory.stringToKey(params.key)
	log.info("Lösche Default-Budget Eintrag: "+item)
	item.delete()
	redirect '/admin/defbudget'
}